// pages/AuthApply/AuthApply.js
const app = getApp();
var utils = require("../../utils/utils.js");
var server = require("../../utils/server.js");
var config = require("../../config.js");
var apis = require("../../utils/api.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    authStatus: app.globalData.userInfo.authStatus,
    dataList: [],
    nodata: true,
    pageNo: 1,
    pageSize: 20,
    isLast: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (app.globalData.userInfo.authStatus != 1) {
      return
    }
    this.setData({
      pageNo: 1,
      dataList: [],
      isLast: false,
      nodata: false
    })
    this.auditListPage()

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  // 查询审核列表
  auditListPage() {
    let _this = this;
    const {
      pageNo,
      pageSize
    } = _this.data
    var prams = {
      pageNo: pageNo,
      pageSize: pageSize,
      entId: app.globalData.userInfo.entId,
      authStatus: 0
    }
    var api = apis.auditListPage;
    server.getRequest(config.xburl, api, prams,
      function (res) {
        let total = res.data.total
        let list = res.data.list
        if (total == 0) {
          let nodata = _this.data.pageNo === 1 ? true : false
          _this.setData({
            isLast: true,
            nodata: nodata,
            dataList: [],
          })
        } else {
          let allData = _this.data.dataList.concat(list)
          let isLast = (total < 10 && _this.data.pageNo === 1) ? true : false
          _this.setData({
            nodata: false,
            dataList: allData,
            isLast: isLast
          })
        }
      },
      function (err) {
        utils.showToast(err)
      })
  },

  apply(e) {
    var _this = this;
    let index = e.currentTarget.dataset.index
    let value = e.currentTarget.dataset.value
    let name = e.currentTarget.dataset.name
    let id = _this.data.dataList[index].id
    let authStatus = value == 'Agree' ? 1 : 0
    let flag = authStatus == 1 ? '同意' : '拒绝'
    wx.showModal({
      title: '温馨提示',
      content: `是否${flag}"${name}"的申请?`,
      complete: (res) => {
        if (res.cancel) {
          return
        }

        if (res.confirm) {
          var prams = {
            authStatus: authStatus,
            userId: id
          }
          var api = apis.audit;
          server.putRequest(config.xburl, api, prams,
            function (res) {
              utils.showToast(authStatus == 1 ? '已同意' : '已拒绝')
              let dataList = _this.data.dataList
              dataList.splice(index, 1)
              let nodata = dataList.length ? false : true
              _this.setData({
                dataList: dataList,
                nodata: nodata
              })
            },
            function (err) {
              utils.showToast(err)
            })
        }
      }
    })
  },

  //分页加载
  scrollToLower() {
    const {
      isLast,
      pageNo,
    } = this.data
    let _this = this
    if (isLast) {
      wx.showToast({
        title: '已经是最后一条数据啦！',
        icon: 'none',
        duration: 1500
      })
    } else {
      _this.setData({
        pageNo: parseInt(pageNo) + 1
      })
      _this.auditListPage()
    }
  },
})